{% extends "admin/base.html" %}

{% block title %}用户管理 - 管理后台 - 万岛潜水{% endblock %}

{% block page_title %}用户管理{% endblock %}

{% block page_actions %}
<button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">
    <i class="fas fa-plus me-1"></i> 添加用户
</button>
{% endblock %}

{% block content %}
<div class="card">
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-6">
                <form method="get" action="{{ url_for('main.admin_users') }}" class="d-flex">
                    <input type="text" name="search" class="form-control me-2" placeholder="搜索用户名或邮箱" value="{{ request.args.get('search', '') }}">
                    <button type="submit" class="btn btn-outline-primary">搜索</button>
                </form>
            </div>
            <div class="col-md-6 text-end">
                <div class="btn-group">
                    <a href="{{ url_for('main.admin_users', filter='all') }}" class="btn btn-outline-secondary {% if request.args.get('filter') == 'all' or not request.args.get('filter') %}active{% endif %}">全部</a>
                    <a href="{{ url_for('main.admin_users', filter='admin') }}" class="btn btn-outline-secondary {% if request.args.get('filter') == 'admin' %}active{% endif %}">管理员</a>
                    <a href="{{ url_for('main.admin_users', filter='member') }}" class="btn btn-outline-secondary {% if request.args.get('filter') == 'member' %}active{% endif %}">会员</a>
                    <a href="{{ url_for('main.admin_users', filter='regular') }}" class="btn btn-outline-secondary {% if request.args.get('filter') == 'regular' %}active{% endif %}">普通用户</a>
                </div>
            </div>
        </div>
        
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>手机</th>
                        <th>注册时间</th>
                        <th>角色</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.email }}</td>
                        <td>{{ user.phone }}</td>
                        <td>{{ user.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                        <td>
                            {% if user.is_admin %}
                            <span class="badge bg-danger">管理员</span>
                            {% elif user.is_member %}
                            <span class="badge bg-success">会员</span>
                            <small class="text-muted">自 {{ user.member_since.strftime('%Y-%m-%d') }}</small>
                            {% else %}
                            <span class="badge bg-secondary">普通用户</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editUserModal{{ user.id }}">
                                    <i class="fas fa-edit"></i>
                                </button>
                                {% if not user.is_admin or current_user.id != user.id %}
                                <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteUserModal{{ user.id }}">
                                    <i class="fas fa-trash"></i>
                                </button>
                                {% endif %}
                            </div>
                            
                            <!-- 编辑用户模态框 -->
                            <div class="modal fade" id="editUserModal{{ user.id }}" tabindex="-1" aria-labelledby="editUserModalLabel{{ user.id }}" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <form method="post" action="{{ url_for('main.admin_edit_user', id=user.id) }}">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="editUserModalLabel{{ user.id }}">编辑用户</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="mb-3">
                                                    <label for="username{{ user.id }}" class="form-label">用户名</label>
                                                    <input type="text" class="form-control" id="username{{ user.id }}" name="username" value="{{ user.username }}" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label for="email{{ user.id }}" class="form-label">邮箱</label>
                                                    <input type="email" class="form-control" id="email{{ user.id }}" name="email" value="{{ user.email }}" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label for="phone{{ user.id }}" class="form-label">手机</label>
                                                    <input type="text" class="form-control" id="phone{{ user.id }}" name="phone" value="{{ user.phone }}">
                                                </div>
                                                <div class="mb-3">
                                                    <label for="password{{ user.id }}" class="form-label">密码</label>
                                                    <input type="password" class="form-control" id="password{{ user.id }}" name="password" placeholder="留空表示不修改">
                                                </div>
                                                <div class="mb-3 form-check">
                                                    <input type="checkbox" class="form-check-input" id="is_admin{{ user.id }}" name="is_admin" {% if user.is_admin %}checked{% endif %}>
                                                    <label class="form-check-label" for="is_admin{{ user.id }}">管理员</label>
                                                </div>
                                                <div class="mb-3 form-check">
                                                    <input type="checkbox" class="form-check-input" id="is_member{{ user.id }}" name="is_member" {% if user.is_member %}checked{% endif %}>
                                                    <label class="form-check-label" for="is_member{{ user.id }}">会员</label>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                <button type="submit" class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- 删除用户确认模态框 -->
                            <div class="modal fade" id="deleteUserModal{{ user.id }}" tabindex="-1" aria-labelledby="deleteUserModalLabel{{ user.id }}" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="deleteUserModalLabel{{ user.id }}">确认删除</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        <div class="modal-body">
                                            <p>您确定要删除用户 "{{ user.username }}" 吗？此操作不可撤销。</p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                            <a href="{{ url_for('main.admin_delete_user', id=user.id) }}" class="btn btn-danger">删除</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        <!-- 分页 -->
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                {% if pagination.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('main.admin_users', page=pagination.prev_num, search=request.args.get('search', ''), filter=request.args.get('filter', '')) }}">上一页</a>
                </li>
                {% else %}
                <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-disabled="true">上一页</a>
                </li>
                {% endif %}
                
                {% for page in pagination.iter_pages() %}
                    {% if page %}
                        {% if page != pagination.page %}
                        <li class="page-item">
                            <a class="page-link" href="{{ url_for('main.admin_users', page=page, search=request.args.get('search', ''), filter=request.args.get('filter', '')) }}">{{ page }}</a>
                        </li>
                        {% else %}
                        <li class="page-item active" aria-current="page">
                            <span class="page-link">{{ page }}</span>
                        </li>
                        {% endif %}
                    {% else %}
                        <li class="page-item disabled">
                            <a class="page-link" href="#">...</a>
                        </li>
                    {% endif %}
                {% endfor %}
                
                {% if pagination.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('main.admin_users', page=pagination.next_num, search=request.args.get('search', ''), filter=request.args.get('filter', '')) }}">下一页</a>
                </li>
                {% else %}
                <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-disabled="true">下一页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
    </div>
</div>

<!-- 添加用户模态框 -->
<div class="modal fade" id="addUserModal" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="{{ url_for('main.admin_add_user') }}">
                <div class="modal-header">
                    <h5 class="modal-title" id="addUserModalLabel">添加用户</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="email" class="form-label">邮箱</label>
                        <input type="email" class="form-control" id="email" name="email" required>
                    </div>
                    <div class="mb-3">
                        <label for="phone" class="form-label">手机</label>
                        <input type="text" class="form-control" id="phone" name="phone">
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_admin" name="is_admin">
                        <label class="form-check-label" for="is_admin">管理员</label>
                    </div>
                    <div class="mb-3 form-check">
                        <input type="checkbox" class="form-check-input" id="is_member" name="is_member">
                        <label class="form-check-label" for="is_member">会员</label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %} 